﻿using System.Threading.Tasks;

namespace Sidvall.Security
{
	public class AuthenticationManager
	{
		#region Public Members

		public Sidvall.Security.IAuthenticationProvider AuthenticationProvider { get; set; }

		#region GetAuthenticationClient

		public IAuthenticationClient GetAuthenticationClient(string address)
		{
			return GetAuthenticationClient(address, null, null, ClientAuthenticationStyle.None);
		}
		public IAuthenticationClient GetAuthenticationClient(string address, string clientId, string clientSecret)
		{
			return GetAuthenticationClient(address, clientId, clientSecret, ClientAuthenticationStyle.BasicAuthentication);
		}
		public IAuthenticationClient GetAuthenticationClient(string address, string clientId, string clientSecret, ClientAuthenticationStyle style)
		{
			return GetAuthenticationClient(new System.Uri(address, System.UriKind.RelativeOrAbsolute), clientId, clientSecret, style);
		}
		public IAuthenticationClient GetAuthenticationClient(System.Uri address)
		{
			return GetAuthenticationClient(address, null, null);
		}
		public IAuthenticationClient GetAuthenticationClient(System.Uri address, string clientId, string clientSecret)
		{
			return GetAuthenticationClient(address, clientId, clientSecret, ClientAuthenticationStyle.BasicAuthentication);
		}
		public IAuthenticationClient GetAuthenticationClient(System.Uri address, string clientId, string clientSecret, ClientAuthenticationStyle style)
		{
			if (this.AuthenticationProvider != null)
				return this.AuthenticationProvider.GetAuthenticationClient(address, clientId, clientSecret, style);
			return null;
		}

		#endregion
		#region GetUserInfoAsync

		public async Task<UserInfo> GetUserInfoAsync(string authorityUserInfoEndpoint, string accessToken)
		{
			if (this.AuthenticationProvider != null)
				return await this.AuthenticationProvider.GetUserInfoAsync(authorityUserInfoEndpoint, accessToken).ConfigureAwait(false);
			return null;
		}

		#endregion
		#region ParseAuthorizeResponseFromAddress

		public AuthorizeResponse ParseAuthorizeResponseFromAddress(string address)
		{
			if (this.AuthenticationProvider != null)
				return this.AuthenticationProvider.ParseAuthorizeResponseFromAddress(address);
			return null;
		}

		#endregion
		#region ParseIdentityTokenFromToken

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
		public IdentityToken ParseIdentityTokenFromToken(string token)
		{
			if (this.AuthenticationProvider != null)
				return this.AuthenticationProvider.ParseIdentityTokenFromToken(token);
			return null;
		}

		#endregion
		#region ToString

		public override string ToString()
		{
			return "AuthenticationManager - Sidvall.Security.IAuthenticationProvider";
		}

		#endregion

		#endregion
		#region Constructors

		public AuthenticationManager(Sidvall.Security.IAuthenticationProvider authenticationProvider)
		{
			this.AuthenticationProvider = authenticationProvider;
		}

		#endregion
	}
}
